﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using Parking.Models;

namespace Parking.Controllers
{ 
    [Authorize(Roles = "Admin")]
    public class PositionsController : Controller
    {
        // con tt
        //private readonly ParkingEntities _db = new ParkingEntities();
        // con CODE FIRST
        private readonly ParkingContext _db = new ParkingContext();

        //
        // GET: /Positions/

        public ViewResult Index()
        {
            return View(_db.Positions.ToList());
        }

        //
        // GET: /Positions/Details/5

        public ActionResult Details(int id = 0)
        {
            Position position = _db.Positions.Find(id);
            if (position == null)
            {
                return HttpNotFound();
            }

            return View(position);
        }

        //
        // GET: /Positions/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Positions/Create

        [HttpPost]
        public ActionResult Create(Position position)
        {
            if (ModelState.IsValid)
            {
                _db.Positions.Add(position);
                _db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(position);
        }
        
        //
        // GET: /Positions/Edit/5
 
        public ActionResult Edit(int id = 0)
        {
            Position position = _db.Positions.Find(id);
            if (position == null)
            {
                return RedirectToAction("Index"); // HttpNotFound();
            }
            return View(position);
        }

        //
        // POST: /Positions/Edit/5

        [HttpPost]
        public ActionResult Edit(Position position)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(position).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(position);
        }

        //
        // GET: /Positions/Delete/5
 
        public ActionResult Delete(int id = 0)
        {
            Position position = _db.Positions.Find(id);
            if (position == null)
            {
                return HttpNotFound();
            }

            return View(position);
        }

        //
        // POST: /Positions/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id = 0)
        {            
            Position position = _db.Positions.Find(id);
            if (position == null)
            {
                return HttpNotFound();
            }
            _db.Positions.Remove(position);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

//        public ActionResult SearchIndex(string id)
        public ActionResult SearchIndex(string searchString)        
        {
            //string searchString = id; // para usar como: {controller}/{action}/{id}

            var positions = from p in _db.Positions
                            select p;

            if (!string.IsNullOrEmpty(searchString))
            {
                positions = positions.Where(s => s.Number.Contains(searchString));
            }

            return View(positions);
        }
        
        public ActionResult SearchLocationAndNumber(string positionsLocations, string searchString)
        {
            var locationList = new List<string>();

            var locationQuery = from d in _db.Positions 
                                orderby d.Location
                           select d.Location;
            locationList.AddRange(locationQuery.Distinct());
            ViewBag.positionsLocations= new SelectList(locationList);

            var positions = from m in _db.Positions
                         select m;

            if (!String.IsNullOrEmpty(searchString))
            {
                positions = positions.Where(s => s.Number.Contains(searchString));
            }

            return View(string.IsNullOrEmpty(positionsLocations) ? positions : positions.Where(x => x.Location == positionsLocations));
        }
        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}